package math.base;

/**
 * @author Saga
 *
 */
public class CCS2d {
    public CCS2d() {
        this.origin = new Point2d();
        this.directX = new Dir2d();
        this.directY = new Dir2d();
        this.directY.setNormalizedCoord(new Coord2d(0.0, 1.0));
    }

    public CCS2d(final Point2d _origin, final Dir2d _directX, final boolean _ccw) {
        this.origin = _origin;
        this.directX = _directX;

        this.directY = new Dir2d();
        if (_ccw) {
            this.directY.setNormalizedCoord(-this.directX.getY(), this.directY.getX());
        } else {
            this.directY.setNormalizedCoord(this.directX.getY(), -this.directY.getX());
        }
    }

    public CCS2d(final Point2d _origin, final Dir2d _directX, final Dir2d _directY) {
        this.origin = _origin;
        this.directX = _directX;

        if (_directX.crossed(_directY) >= 0.0) {
            this.directY.setNormalizedCoord(-directX.getY(), directX.getX());
        } else {
            this.directY.setNormalizedCoord(directX.getY(), -directX.getX());
        }
    }

    public final void setOrigin(final Point2d _origin) {
        this.origin = _origin;
    }

    public final Point2d getOrigin() {
        return this.origin;
    }

    public final void setDirectX(final Dir2d _directX) {
        this.directX = _directX;
        if (_directX.crossed(this.directY) >= 0.0) {
            this.directY.setNormalizedCoord(-directX.getY(), directX.getX());
        } else {
            this.directY.setNormalizedCoord(directX.getY(), -directX.getX());
        }
    }

    public final Dir2d getDirectX() {
        return this.directX;
    }

    public final void setDirectY(final Dir2d _directY) {
        this.directY = _directY;
        if (_directY.crossed(this.directX) >= 0.0) {
            this.directX.setNormalizedCoord(-directY.getY(), directY.getX());
        } else {
            this.directX.setNormalizedCoord(directY.getY(), -directY.getX());
        }
    }

    public final Dir2d getDirectY() {
        return this.directY;
    }

    public final void setOrthogonalDirect(final Dir2d _directX, final Dir2d _directY) {
        this.directX = _directX;
        this.directY = _directY;
    }

    public final void xReverse() {
        this.directX.reverse();
    }

    public final CCS2d xReversed() {
        return new CCS2d(this.origin, this.directX.reversed(), this.directY);
    }

    public final void yReverse() {
        this.directY.reverse();
    }

    public final CCS2d yReversed() {
        return new CCS2d(this.origin, this.directX.reversed(), this.directY);
    }

    @Override
    public final CCS2d clone() {
        final CCS2d output = new CCS2d();
        output.origin = this.origin.clone();
        output.directX = this.directX.clone();
        output.directY = this.directY.clone();

        return output;
    }

    private Point2d origin;
    private Dir2d directX;
    private Dir2d directY;
}
